﻿/*  ----------------------------------------------------------------------------
 *  Project Intercept
 *  ----------------------------------------------------------------------------
 *  System.Common
 *  ----------------------------------------------------------------------------
 *  File:        Dates.cs
 *  Authors:     Dremation, Vendetta
 *  ----------------------------------------------------------------------------
 *  GNU General Public License version 2 (GPLv2)
 */

namespace System.Common
{
    public class Dates
    {
        static public readonly DateTime UnixEpochStart = new DateTime(1970, 1, 1, 0, 0, 0);

        public static int GetCurrentUnixTime()
        {
            return (int)(DateTime.UtcNow - UnixEpochStart).TotalSeconds; ;
        }
        
        public static DateTime ConvertFromUnixTimestamp(int timestamp)
        {
            return UnixEpochStart.AddSeconds(timestamp);
        }

        public static int ConvertToUnixTimestamp(DateTime date)
        {
            TimeSpan diff = date - UnixEpochStart;
            return (int)Math.Floor(diff.TotalSeconds);            
        }
        public enum PartsOfTime
        {
            ///<summary> Gets the Seconds from the specified DateTime</summary>
            Seconds,
            /// <summary>Gets the Minutes from the specified DateTime</summary>
            Minutes,
            /// <summary>Gets the Hours from the specified DateTime</summary>
            Hours,
            /// <summary>Gets the Days from the specified DateTime</summary>
            Days,
        }
        /// <summary>
        /// Gets Information from the date specified to DateTime.Now.
        /// </summary>
        /// <param name="date">The DateTime timestamp you wish to get information from</param>
        /// <param name="piece">The piece of information you wish to get (Seconds,Minutes,Hours,Days)</param>
        /// <returns>Returns the specified piece of information as a double (or -1 on failure)</returns>
        public static double InformationFromDate(DateTime date, PartsOfTime piece)
        {
            DateTime now = DateTime.Now;
            TimeSpan ts = now - date;
            switch (piece)
            {
                case PartsOfTime.Days:
                    return ts.TotalDays;
                case PartsOfTime.Hours:
                    return ts.TotalHours;
                case PartsOfTime.Minutes:
                    return ts.TotalMinutes;
                case PartsOfTime.Seconds:
                    return ts.TotalSeconds;
                default:
                    return -1;
            }
        }
    }
}
